package krow.dev.gunpobus.view.fragment;

import krow.dev.gunpobus.view.LoadViewWrapper;
import krow.dev.gunpobus.view.RetryViewWrapper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class BaseFragment extends Fragment{

	private RetryViewWrapper mRetryViewWrapper;
	private LoadViewWrapper mLoadViewWrapper;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		ViewGroup view = (ViewGroup) getView();
		if(view != null){
			
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			
			mRetryViewWrapper = new RetryViewWrapper(getActivity());
			mRetryViewWrapper.setRetryClickListener(mRetryClickListener);
			view.addView(mRetryViewWrapper.getView(), layoutParams);
		
			mLoadViewWrapper = new LoadViewWrapper(getActivity());
			view.addView(mLoadViewWrapper.getView(), layoutParams);
		}
	}
	
	protected void onRetry(){
		if(mRetryViewWrapper != null){
			mRetryViewWrapper.setVisibility(View.GONE);
		}
	}
	
	protected void showRetryAlert(){
		mRetryViewWrapper.setVisibility(View.VISIBLE);
	}
	
	protected void setLoadBarVisibility(int visibility){
		mLoadViewWrapper.setVisibility(visibility);
	}
	
	private View.OnClickListener mRetryClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View view) {
			onRetry();
		}
	};
}
